package com.zllkj.mm.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Toast;

import com.zllkj.mm.AppManager;
import com.zllkj.mm.R;
import com.zllkj.mm.module.UIHelper;
import com.zllkj.mm.util.StatusBarUtil;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import org.jetbrains.annotations.NotNull;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

public abstract class BaseActivity extends AppCompatActivity {

    // 防抖动(防止点击速度过快)，这里是防止动画过程中，按返回键取消加载Fragment
    private boolean mFragmentClickable = true;
    private Unbinder unbinder;

    //得到填充布局用以加载
    protected abstract int getContentViewId();

    //初始化控件
    protected void initView() {
    }

    //加载数据
    protected void loadData() {
    }




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加当前activity到堆栈式管理
        AppManager.getAppManager().addActivity(this);
        if (initBundle(getIntent().getExtras())) {
            setContentView(getContentViewId());
            unbinder = ButterKnife.bind(this);
            initView();
            loadData();
            //StatusBarUtil.transparencyBar(this);
            //  StatusBarUtil.StatusBarLightMode(this);
            getWindow() .clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(getResources().getColor(R.color.color_background));
            Toolbar tool_bar = findViewById(R.id.tool_bar);
            if(tool_bar != null){

                setSupportActionBar(tool_bar);
                getSupportActionBar().setDisplayShowTitleEnabled(false);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                tool_bar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
            }



        } else {
            finish();
        }





        registerReceiver(tokenFailedBR,new IntentFilter("token_fail"));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        AppManager.getAppManager().removeActivity(this);

    }


    private BroadcastReceiver tokenFailedBR = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals("token_fail")){
                    UIHelper.showLoginPage(BaseActivity.this);
                }

        }
    };


    protected boolean initBundle(Bundle bundle) {
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            // 防抖动(防止点击速度过快)，这里是防止动画过程中，按返回键取消加载Fragment
            if (!mFragmentClickable) {
                setFragmentClickable(true);
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    /**
     * 防抖动(防止点击速度过快)，这里是防止动画过程中，按返回键取消加载Fragment
     */
    void setFragmentClickable(boolean clickable) {
        mFragmentClickable = clickable;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 获取到Activity下的Fragment
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments == null)
        {
            return;
        }
        // 查找在Fragment中onRequestPermissionsResult方法并调用
        for (Fragment fragment : fragments)
        {
            if (fragment != null)
            {
                // 这里就会调用我们Fragment中的onRequestPermissionsResult方法
                fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    }

    // rx总线
    private CompositeSubscription compositeSubscription = new CompositeSubscription();

    //rx总线里添加订阅
    public void addSubscribe(Subscription subscription) {
        compositeSubscription.add(subscription);
    }

    //rx取消订阅
    public void unSubscribe() {
        if (compositeSubscription != null) {
            compositeSubscription.unsubscribe();
            compositeSubscription = null;
        }
    }

}
